package control.hall
{
	import core.Control;
	import core.Model;
	import core.View;
	
	import events.HallEvent;
	import events.LoginEvent;
	import events.ModulEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import global.GameState;
	
	import model.load.XmlLoadMessage;
	
	import mx.events.ModuleEvent;
	
	import view.components.ModulLoader;

	public class PlayerInfoModulControl
	{
		private var _view:View = View.getInstance();
		private var _model:Model = Model.getInstance();
		private var _control:Control = Control.getInstance();
		private var playerInfoModul:ModulLoader;
		private var info:XML;
		private var loginSuccess:Boolean;
		
		public function PlayerInfoModulControl()
		{
			initModul();
			loadModul("PlayerInfoModul.swf");
			initListener();
		}
		
		private function initListener():void
		{
			_model.hallModel.addEventListener(HallEvent.PLAYER_INFO_COMPLETE,onHallPlayerInfoComplete);
			
			if (GameState.DEBUG_STATE == 1)
			{
				_model.loginModel.addEventListener(LoginEvent.ACCESSSERVER_LOGINING,onAccessLoginSuccess);
			}
			else
			{
				_model.loginModel.addEventListener(LoginEvent.SUCCESS,onAccessLoginSuccess);
			}
		}
		
		private function onAccessLoginSuccess(event:LoginEvent):void
		{
			if (playerInfoModul.isComplete)
			{
				loadServerMsg();
			}
			loginSuccess = true;
		}
		
		private function onHallPlayerInfoComplete(event:HallEvent):void
		{
			info = event.xml;
			if (playerInfoModul.isComplete)
			{
				playerInfoModul.content.update(info);
				
				if (info.info.@isFirstLogin.toString() == "1")
				{
					_control.helpPanelControl.open();
				}
			}
		}
		
		private function initModul():void
		{
			playerInfoModul = new ModulLoader(364,157);
			playerInfoModul.x = 410;
			playerInfoModul.y = 14;
			_view.hallLayer.addChild(playerInfoModul);
		}
		
		public function loadModul(url:String):void
		{
			playerInfoModul.load(url);
			playerInfoModul.addEventListener(Event.COMPLETE,onComplete);
		}
		
		private function onComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onComplete);
			playerInfoModul.content.init();
			if (info)
			{
				playerInfoModul.content.update(info);
			}
			addListener();
			
			if (loginSuccess)
			{
				loadServerMsg();
			}
		}
		
		private function loadServerMsg():void
		{
			var url:String  = GameState.getServerLink() + "25&key=" + GameState.snsKey;
			var xmlMsg:XmlLoadMessage = new XmlLoadMessage(onServerMsgBack);
			xmlMsg.isImmediate = true;
			xmlMsg.url = url;
			xmlMsg.send();
		}
		
		private function onServerMsgBack(xml:XML,id:int):void
		{
			playerInfoModul.content.updateServerMsg(xml);
		}
		
		private function addListener():void
		{
			playerInfoModul.content.addEventListener(ModulEvent.RE_FRESH,onModulRefresh);
			playerInfoModul.content.addEventListener(ModulEvent.FEED_BACK,onModulFeedBack);
			playerInfoModul.content.addEventListener(ModulEvent.ITEM,onItemBut);
			playerInfoModul.content.addEventListener(ModulEvent.REWARD,onReward);
			playerInfoModul.content.addEventListener(ModulEvent.TASK,onTask);
			playerInfoModul.content.addEventListener(ModulEvent.BANK,onBank);
			playerInfoModul.content.addEventListener(ModulEvent.VIPSET,onVipSet);
		}
		
		private function onVipSet(event:ModulEvent):void
		{
			_control.vipSetPanelControl.open();
		}
		
		private function onBank(event:ModulEvent):void
		{
			_control.bankPanelControl.open();
		}
		
		private function onTask(event:ModulEvent):void
		{
			_control.taskPanelControl.open();
		}
		
		private function onReward(event:ModulEvent):void
		{
			_control.rewardPanelControl.open();
		}
		
		private function onItemBut(event:ModulEvent):void
		{
			_control.itemPanelControl.open();
		}
		
		private function onModulRefresh(event:ModulEvent):void
		{
			refreshInfo();
		}
		
		public function refreshInfo():void
		{
			_model.hallModel.loadPlayerInfo();
			loadServerMsg();
		}
		
		private function onModulFeedBack(event:ModulEvent):void
		{
			_control.feedPanelControl.open();
		}
		
		public function disableReward():void
		{
			playerInfoModul.content.disableReward();
		}
		
		
	}
}